import store from '@/store'

type PermissionArray = string[]

/**
 * 字符权限校验
 * @param {Array} value 校验值
 * @returns {Boolean}
 */
export function checkPermi(value: PermissionArray): boolean {
  if (value) {
    const permissions = (store.getters?.permissions as PermissionArray) || []
    const allPermission = '*:*:*'
    return permissions.some((permission) => {
      return allPermission === permission || value.includes(permission)
    })
  } else {
    return false
  }
}

/**
 * 角色权限校验
 * @param {Array} value 校验值
 * @returns {Boolean}
 */
export function checkRole(value: PermissionArray): boolean {
  if (value && value instanceof Array && value.length > 0) {
    const roles = (store.getters?.roles as PermissionArray) || []
    const superAdmin = 'admin'

    return roles.some((role) => {
      return superAdmin === role || value.includes(role)
    })
  } else {
    console.error(`need roles! Like checkRole="['admin','editor']"`)
    return false
  }
}
